﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyBlog.Model;
using MyBlog.DataAccess;
using System.Linq.Expressions;
using AutoMapper;
using MyBlog.Common;
using MyBlog.BusinessInterface;
using MyBlog.DataEntity;
using MyBlog.DataAccessInterface;

namespace MyBlog.Business
{
    public class BusinessAccount : IBusinessAccount
    {
        IDALAccount _dalAccount;
        IDALHeader _dalHeader;
        IUnitOfWork _unitWork;
        public BusinessAccount(IDALAccount dalAccount, IDALHeader dalHeader, IUnitOfWork unitWork)
        {
            _dalAccount = dalAccount;
            _dalHeader = dalHeader;
            _unitWork = unitWork;
        }
        public void Add(AccountModel model)
        {
            model.Account = model.Account.ToLower();
            model.Filter(x => x.UserName, x => x.NickName, x => x.Profession, x => x.Native);
            model.Password = model.Password.MD5_16().EncryptDES().MD5_32();
            var dataEntity = Mapper.Map<Tbl_Account>(model);
            if (model.Header?.ImageContent != null)
            {
                var header = Mapper.Map<Tbl_Header>(model.Header);
                _dalHeader.Add(header);
                dataEntity.HeadId = header.ID;
            }
            dataEntity.Sex = model.Sex ?? true;
            dataEntity.Email = dataEntity.Email.Replace("third@third.com", "");//第三方登录默认设置邮箱，需要去除
            _dalAccount.Add(dataEntity);
            _unitWork.Commit();
            model.ID = dataEntity.ID;
        }

        public void Update(AccountModel model)
        {
            model.Filter(x => x.UserName, x => x.NickName, x => x.Profession, x => x.Native);
            var dataEntity = _dalAccount.Get(model.Account);
            dataEntity.Birthday = model.Birthday;
            dataEntity.NickName = model.NickName;
            dataEntity.UserName = model.UserName;
            dataEntity.Sex = model.Sex;
            dataEntity.Profession = model.Profession;
            dataEntity.PersonalProfile = model.PersonalProfile;
            dataEntity.Native = model.Native;
            dataEntity.Email = model.Email;
            dataEntity.Education = model.Education;
            dataEntity.Motto = model.Motto;
            dataEntity.Template = model.Template.ToString();
            _unitWork.Commit();
        }
        public void AddRange(IEnumerable<AccountModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_Account>>(models);
            _dalAccount.AddRange(dataEntitys);
            _unitWork.Commit();
        }

        public void DeleteRange(IEnumerable<AccountModel> models)
        {
            var dataEntitys = Mapper.Map<List<Tbl_Account>>(models);
            _dalAccount.DeleteRange(dataEntitys);
            _unitWork.Commit();
        }

        public List<AccountModel> GetAll(bool isTracking = true)
        {
            var entities = _dalAccount.GetAll(isTracking);
            return Mapper.Map<List<AccountModel>>(entities);
        }

        public void Delete(params object[] primaryKey)
        {
            _dalAccount.Delete(primaryKey);
            _unitWork.Commit();
        }

        public AccountModel Get(params object[] primaryKey)
        {
            var dataEntity = _dalAccount.Get(primaryKey);
            return Mapper.Map<AccountModel>(dataEntity);
        }

        public void Delete(AccountModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_Account>();
            if (model.ID > 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            _dalAccount.Delete(where);
            _unitWork.Commit();
        }

        public AccountModel Get(AccountModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_Account>();
            if (model.ID > 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            if (!string.IsNullOrWhiteSpace(model.Account))
            {
                where = where.And(x => x.Account.Trim().ToLower() == model.Account.Trim().ToLower());
            }
            if (!string.IsNullOrWhiteSpace(model.Password))
            {
                var pass = model.Password.MD5_16().EncryptDES().MD5_32();
                where = where.And(x => x.Password == model.Password || x.Password == pass);
            }
            if (!string.IsNullOrWhiteSpace(model.Email))
            {
                where = where.And(x => x.Email.Trim().ToLower() == model.Email.Trim().ToLower());
            }
            var dataEntity = _dalAccount.Get(where);
            return Mapper.Map<AccountModel>(dataEntity);
        }

        public List<AccountModel> GetAll(AccountModel model, bool isTracking = true)
        {
            var where = LinqExpressionBuilder.True<Tbl_Account>();
            if (!string.IsNullOrWhiteSpace(model.Account))
            {
                where = where.And(x => x.Account.ToLower() == model.Account.Trim());
            }
            var entities = _dalAccount.GetAll(where, isTracking);
            return Mapper.Map<List<AccountModel>>(entities);
        }

        public List<AccountModel> GetAll(AccountModel model, int page, int size, out int total, bool isTracking = true)
        {
            throw new NotImplementedException();
        }

        public void UploadUserHead(string account, HeaderModel head)
        {
            var headerEntity = new Tbl_Header { ImageContent = head.ImageContent, Size = head.Size, ContentType = head.ContentType };
            _dalHeader.Add(headerEntity);
            var user = _dalAccount.Get(account);
            if (user.HeadId.HasValue)
            {
                _dalHeader.Delete(user.HeadId);
            }
            user.HeadId = headerEntity.ID;
            _unitWork.Commit();
        }

        public void SaveUserWantSay(string account, string wantsay)
        {
            var user = _dalAccount.Get(account);
            user.WantSay = wantsay;
            _unitWork.Commit();
        }

        public void UpdatePassword(AccountModel model)
        {
            var user = _dalAccount.Get(model.Account);
            var pass = model.Password.MD5_16().EncryptDES().MD5_32();
            user.Password = pass;
            _unitWork.Commit();
        }

        public void Active(string account)
        {
            var user = _dalAccount.Get(account);
            user.IsActive = true;
            _unitWork.Commit();
        }
    }
}
